// pages/message/index.js
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
const $http = require('../../request/index');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    message: {
      path: "https://www.hualigs.cn/image/60b5f056453e7.jpg",
      name: " ",
      studentId: " ",
      phone: " ",
      school: "",
      department: "",
      class: "",
    },
    // 学校相关信息
    school: {
      name: [],
      info: [],
      index: 0,
      id: "",
    },
    class: {
      name: [],
      info: [],
      index: 0,
      id: "",
    },
    department: {
      departmentIndex: 0,
      departmentInfo: [],
      departmentName: [],
      departmentId: "",
    },
    currentMessage: {
      schoolContent: "",
      classContent: "",
      departmentContent: "",
    },


  },
  onLoad: function (options) {
    this.setUserMessage();
  },
  onshow: function () {
    // 当修改页面结束后 返回该页面 需要重新设置 用户修改好的内容
    this.setUserMessage();
  },

  // 在用户初次打开时 若授权了那么 显示头像,名称 性别
  setUserMessage() {
    let app = getApp();
    this.setData({
      ["message.path"]: app.userInfo.avatarUrl,
      ["message.name"]: app.userInfo.name,
      ["message.studentId"]: app.userInfo.number || "",
      ["message.phone"]: app.userInfo.phone || "",
      ["message.school"]: app.userInfo.schoolName || "",
      ["message.department"]: app.userInfo.deptName || "",
      ["message.class"]: app.userInfo.className || "",
    })
    console.log("UserData", this.data.message)
  },

  name_change() {
    var name = this.data.message.name
    wx.navigateTo({
      url: `/pages/modify_information/index?name=${name}`,
      success: (result) => { },
    });
  },
  phone_change() {
    var phone = this.data.message.phone;
    wx.navigateTo({
      url: `/pages/modify_information/index?phone=${phone}`,
      success: (result) => { },
    });
  },
  studentId_change() {
    var studentId = this.data.message.studentId
    wx.navigateTo({
      url: `/pages/modify_information/index?studentId=${studentId}`,
      success: (result) => { },
    });
  },

  school_change() {
    var SchoolName = "请填写您的学校信息"
    wx.navigateTo({
      url: `/pages/modify_information/index?SchoolName=${SchoolName}`,
      success: (result) => { },
    });
  },
  department_change() {
    var DepartmentName = "请填写您的学校信息"
    wx.navigateTo({
      url: `/pages/modify_information/index?DepartmentName=${DepartmentName}`,
      success: (result) => { },
    });
  },
  class_change() {
    var ClassName = "请填写您的班级信息"
    wx.navigateTo({
      url: `/pages/modify_information/index?ClassName=${ClassName}`,
      success: (result) => { },
    });
  },
  submitMessage() {
    let app = getApp();
    console.log("app", app.userInfo);
    $http.request("PUT", `/front/user/complete`, {
      avatarUrl: app.userInfo.avatarUrl,
      phone: app.userInfo.phone,
      number: app.userInfo.id,
      name: app.userInfo.name,
      gender: app.userInfo.gender,
      classId: app.userInfo.classId,
    }).then(res => {
      console.log("res", res)
      if (res.data.code == 200) {
        wx.reLaunch({
          url: '/pages/user/index',
          success: (result) => {
            wx.showToast({
              title: '信息完善成功',
              icon: 'success',
              duration: 1500
            })
            Notify({ type: 'success', message: '信息完善成功' });
          },
        })
      }
    })
  }
})
